package com.pfizer.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pfizer.PicView.AsyncImageLoader;
import com.pfizer.pfizerExpertForPad.R;

/**
 * HandoutListAdapter--<b>文章信息的數據適配器，用於綁定listview</b>--
 * 
 * @see android.widget.BaseAdapter
 * @author krui
 * @version 1.0 2011/11/24
 */
public class HandoutListAdapter extends BaseAdapter {

	//定義一個list
	List<Map<String, Object>> list;
	HashMap<Integer,Bitmap> dataCache;
	Context mContext; //定義上下文參數
	private int selectItem = 0; // 定義初始選定位置

	public HandoutListAdapter() {
		// TODO Auto-generated constructor stub
	}
/**
 * 帶參數的構造函數
 * @param list（數據源）
 * @param mContext（上下文參數）
 */
	public HandoutListAdapter(List<Map<String, Object>> list, Context mContext) {
		this.list = list;
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		HandoutViewHolder vh = null;
		if (convertView == null) {
			vh = new HandoutViewHolder();//定義一個viewHolder提高效率
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.li_handout, null);//從佈局文件中過濾出佈局以得到各個控件
			vh.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
			vh.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
			vh.tvDesp = (TextView) convertView.findViewById(R.id.tvDesp);
			convertView.setTag(vh);//設置一個tag用於緩存
		} else {
			vh = (HandoutViewHolder) convertView.getTag();
		}
		/*
		try
		{
		vh.ivIcon.setImageBitmap(safeDecodeStream((Uri)list.get(position).get("Icon"), 78, 76));
		}
		catch(FileNotFoundException ex)
		{
			;
		}
		*/
		//vh.ivIcon.setImageURI((Uri) list.get(position).get("Icon"));
		//vh.ivIcon.setTag((String) list.get(position).get("Icon"));
		//new AsyncViewTask().execute(vh.ivIcon);//异步加载图片  
		setViewImage(vh.ivIcon, (String) list.get(position).get("Icon"));
		vh.tvDesp.setText((String) list.get(position).get("Title"));
		vh.tvTime.setText((String) list.get(position).get("LstUpdTime"));

		// 更换Item背景
		if (position == selectItem) {
			convertView.setBackgroundResource(R.drawable.listitem_bg_sel);
		} else {
			convertView.setBackgroundResource(R.drawable.listitem_bg_normal);
		}

		return convertView;
	}

	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}

	public void setViewImage(final ImageView v, String url) {
		// 如果只是单纯的把图片显示，而不进行缓存。直接用下面的方法拿到URL的Bitmap就行显示就OK
		AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
		asyncImageLoader.loadDrawable(url, 1,
				new AsyncImageLoader.ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,
							String imageUrl) {
						if (imageDrawable != null
								&& imageDrawable.getIntrinsicWidth() > 0) {
							v.setImageDrawable(imageDrawable);
						} 
					}
				});
	}
}